package com.igeekhome.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeekhome.biz.RoleManageService;
import com.igeekhome.pojo.CustomerService;
import com.igeekhome.pojo.Notice;
import com.igeekhome.pojo.RoleManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 角色管理 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-16
 */
@Controller
@RequestMapping("/roleManage")
public class RoleManageController {

    @Autowired
    private RoleManageService roleManageService;

    //客服信息管理
    @RequestMapping("/roleManage")
    public String index(Model model, @RequestParam("current") Integer current, @RequestParam("size") Integer size){

        initRoleManageService(model,current,size);
        model.addAttribute("main","角色管理主页");
        return "rolemanage";
    }


    @RequestMapping("/del")
    public String del(Model model,RoleManage roleManage,@RequestParam("current") Integer current, @RequestParam("size") Integer size){

        this.roleManageService.removeById(roleManage.getRoleid());
        initRoleManageService(model,current,size);
        model.addAttribute("main","角色删除");
        return "rolemanage";
    }


    //得到要修改的对象信息，然后跳转到修改页面
    @RequestMapping("/update")
    public String update(Model model, RoleManage roleManage, @RequestParam("current") Integer current, @RequestParam("size") Integer size){

        RoleManage roleManage1 = roleManageService.getById(roleManage.getRoleid());
        model.addAttribute("role",roleManage1);
        model.addAttribute("current",current);
        model.addAttribute("size",size);
        model.addAttribute("main","角色编辑");
        return "updaterole";
    }

    //修改完成,并跳转到公告管理界面
    @RequestMapping("/updateVerify")
    public String updateVerify(RoleManage roleManage, Model model,@RequestParam("current") Integer current, @RequestParam("size") Integer size){

        this.roleManageService.updateById(roleManage);
        initRoleManageService(model,current,size);
        model.addAttribute("main","角色编辑");
        return "rolemanage";

    }

    public void initRoleManageService(Model model, Integer current, Integer size)
    {

        IPage<RoleManage> page=new Page<>(current,size);
        IPage<RoleManage> page1 = this.roleManageService.page(page);
        List<RoleManage> list = page1.getRecords();
        long pagesCount = page1.getPages();
        model.addAttribute("list",list);
        model.addAttribute("pagesCount",pagesCount);
    }
}

